<?php
/**
 * -----------------------------------------------------
 *
 * models Proimg_model
 *
 * @package     models
 * @subpackage  
 * @category    Proimg_model  -  产品图片模型类
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Proimg_model extends CI_Model
{    
	public static $error;
	const TABLE = 'xc_pro_img'; // 表名

	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加数据
	 */
	public function insert($data)
	{
		if($this->db->insert(self::TABLE,$data)){
			return true;
		}else{
			return false;
		}
	}

             
	/**
	 * 查询所有的数据，并且分页
	 */	
	public function get_all_data_and_page($limit,$pid=null)
	{
		$where = '';
		if($pid){
			$where = 'where a.pro_id = '.$pid.' and img_status = 1';
		}
		$sql = 'select a.*,b.pro_name
		from '.self::TABLE.' as a left join xc_products as b
		on a.pro_id=b.pro_id 
		'.$where.'
		order by a.img_sort desc '.$limit;
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	/**
	 * 查询共有多少数据
	 */
	public function get_num_rows($pid=null)
	{
		if($pid){
			$this->db->where('pro_id',$pid);
		}
		$query = $this->db->get(self::TABLE);
		return $query->num_rows();
	}

	/**
	 * 编辑数据信息
	 */
	public function update($data,$id)
	{
		if($this->db->update(self::TABLE,$data,'img_id = '.$id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据id查询数据信息
	 */
	public function get_a_data_by_aid($aid)
	{
		$this->db->where('img_id',$aid);
		$this->db->select('xc_pro_img.*,xc_products.pro_name');
		$this->db->join('xc_products', 'xc_pro_img.pro_id = xc_products.pro_id');
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * 根据id删除数据
	 */
	public function delete($id)
	{
		$this->db->where('attr_id',$id);
		if($this->db->delete(self::TABLE)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * [get_date_by_pid 根据产品id获取属性]
	 * @param  [type] $pid [description]
	 * @return [type]      [description]
	 */
	public function get_date_by_pid($pid,$limit){
		$sql = 'select a.*,b.pro_name
		from '.self::TABLE.' as a left join xc_products as b
		on a.pro_id=b.pro_id 
		where a.attr_status = 1 and a.pro_id = '.$pid.' 
		order by a.attr_sort asc '.$limit;

		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	

}